package com.wgchao.identityrightsmanagement.application.service;

import com.wgchao.identityrightsmanagement.domain.resource.model.*;
import com.wgchao.identityrightsmanagement.domain.resource.repository.FuncRepository;
import com.wgchao.identityrightsmanagement.domain.resource.repository.MenuRepository;
import com.wgchao.identityrightsmanagement.domain.resource.repository.ProjectRepository;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: MenuApplicationSerive
 * @author: wgchao
 * @createTime: 2020/6/11 12:50 PM
 * @Description: 菜单资源管理
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MenuApplicationSerive {
    @Autowired
    private ProjectRepository projectRepository;
    @Autowired
    private MenuRepository menuRepository;
    @Autowired
    private FuncRepository funcRepository;

    /**
     * 添加菜单资源
     * @param proMark 项目标识
     * @param creator
     * @param operator
     */
    public void addMenuToProject(String proMark, MenuResourceCreator creator, String operator){
        ProjectResources projectResources = projectRepository.findById(proMark);
        if(projectResources == null){
            throw new ObjectNotExistException("项目不存在");
        }

        MenuResources menuResources = MenuResources.addmenu(projectResources, creator);
        menuRepository.add(menuResources, operator);
    }

    /**
     * 修改菜单信息
     * @param menuMark
     * @param updator
     * @param operator
     */
    public void modifyMenuInfo(String menuMark, MenuResourcesUpdator updator, String operator){

        MenuResources menuResources = menuRepository.findById(menuMark);
        if(menuResources == null){
            throw new ObjectNotExistException("菜单不存在");
        }
        ProjectResources projectResources = projectRepository.findById(menuResources.getProMark());
        if(projectResources == null){
            throw new ObjectNotExistException("项目不存在");
        }
        menuResources.modifyMenuInfo(projectResources, updator);
        menuRepository.add(menuResources, operator);
    }

    /**
     * 删除菜单资源
     * @param menuMark
     * @param operator
     */
    public void removeMenu(String menuMark, String operator){
        MenuResources menuResources = menuRepository.findById(menuMark);
        if(menuResources == null){
            throw new ObjectNotExistException("菜单不存在");
        }
        menuRepository.remove(menuResources, operator);
    }

    /**
     * 为菜单添加功能资源
     * @param menuMark
     * @param creator
     * @param operator
     */
    public void addFuncToMenu(String menuMark, FuncResourcesCreator creator, String operator){
        MenuResources menuResources = menuRepository.findById(menuMark);
        if(menuResources == null){
            throw new ObjectNotExistException("菜单不存在");
        }
        FunctionResources functionResources = menuResources.addFunc(creator);
        funcRepository.add(functionResources, operator);
    }

    /**
     * 删除功能资源
     * @param menuMark
     * @param funcMark
     * @param operator
     */
    public void removeFunc(String menuMark, String funcMark, String operator){
        FunctionResources functionResources = funcRepository.findById(new FuncIdentify(menuMark, funcMark));
        if(functionResources == null){
            throw new ObjectNotExistException("要删除的菜单功能不存在");
        }
        funcRepository.remove(functionResources, operator);
    }

    /**
     * 修改菜单功能名称
     * @param menuMark
     * @param funcMark
     * @param funcResourcesUpdator
     */
    public void modifyFuncName(String menuMark, String funcMark, FuncResourcesUpdator funcResourcesUpdator, String operator){
        MenuResources menuResources = menuRepository.findById(menuMark);
        if(menuResources == null){
            throw new ObjectNotExistException("菜单不存在");
        }
        FunctionResources functionResources = funcRepository.findById(new FuncIdentify(menuMark, funcMark));
        if(functionResources == null){
            throw new ObjectNotExistException("要修改的菜单功能不存在");
        }
        functionResources = menuResources.modifyFuncName(functionResources, funcResourcesUpdator);
        funcRepository.add(functionResources, operator);
    }

}
